home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / inspector.jar / content / inspector / utils.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  5.7 KB  |  147 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Netscape Public License
  5.  * Version 1.1 (the "License"); you may not use this file except in
  6.  * compliance with the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/NPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is 
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Joe Hewitt <hewitt@netscape.com> (original author)
  23.  *
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /***************************************************************
  40. * Inspector Utils ----------------------------------------------
  41. *  Common functions and constants used across the app.
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  43. * REQUIRED IMPORTS:
  44. * chrome://inspector/content/jsutil/xpcom/XPCU.js
  45. * chrome://inspector/content/jsutil/rdf/RDFU.js
  46. ****************************************************************/
  47.  
  48. //////////// global constants ////////////////////
  49.  
  50. const kInspectorNSURI = "http://www.mozilla.org/inspector#";
  51. const kXULNSURI = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  52. const kHTMLNSURI = "http://www.w3.org/1999/xhtml";
  53.  
  54. var InsUtil = {
  55.   /******************************************************************************
  56.   * Convenience function for calling nsIChromeRegistry::convertChromeURL 
  57.   *******************************************************************************/
  58.   convertChromeURL: function(aURL)
  59.   {
  60.     var uri = XPCU.getService("@mozilla.org/network/simple-uri;1", "nsIURI");
  61.     uri.spec = aURL;
  62.     var reg = XPCU.getService("@mozilla.org/chrome/chrome-registry;1", "nsIChromeRegistry");
  63.     return reg.convertChromeURL(uri);
  64.   },
  65.  
  66.   /******************************************************************************
  67.   * Convenience function for getting a literal value from nsIRDFDataSource::GetTarget
  68.   *******************************************************************************/
  69.   getDSProperty: function(/*nsISupports*/ aDS, /*String*/ aId, /*String*/ aPropName) 
  70.   {
  71.     var ruleRes = gRDF.GetResource(aId);
  72.     var ds = XPCU.QI(aDS, "nsIRDFDataSource"); // just to be sure
  73.     var propRes = ds.GetTarget(ruleRes, gRDF.GetResource(kInspectorNSURI+aPropName), true);
  74.     propRes = XPCU.QI(propRes, "nsIRDFLiteral");
  75.     return propRes.Value;
  76.   },
  77.   
  78.   /******************************************************************************
  79.   * Convenience function for persisting an element's persisted attributes.
  80.   *******************************************************************************/
  81.   persistAll: function(aId)
  82.   {
  83.     var el = document.getElementById(aId);
  84.     if (el) {
  85.       var attrs = el.getAttribute("persist").split(" ");
  86.       for (var i = 0; i < attrs.length; ++i) {
  87.         document.persist(aId, attrs[i]);
  88.       }
  89.     }
  90.   }
  91. };
  92.  
  93. // ::::::: debugging utilities :::::: 
  94.  
  95. function debug(aText)
  96. {
  97.   // XX comment out to reduce noise 
  98.   consoleDump(aText);
  99.   //dump(aText);
  100. }
  101.  
  102. // dump text to the Javascript Console
  103. function consoleDump(aText)
  104. {
  105.   var csClass = Components.classes['@mozilla.org/consoleservice;1'];
  106.   var cs = csClass.getService(Components.interfaces.nsIConsoleService);
  107.   cs.logStringMessage(aText);
  108. }
  109.  
  110. function dumpDOM(aNode, aIndent)
  111. {
  112.   if (!aIndent) aIndent = "";
  113.   debug(aIndent + "<" + aNode.localName + "\n");
  114.   var attrs = aNode.attributes;
  115.   var attr;
  116.   for (var a = 0; a < attrs.length; ++a) { 
  117.     attr = attrs[a];
  118.     debug(aIndent + "  " + attr.nodeName + "=\"" + attr.nodeValue + "\"\n");
  119.   }
  120.   debug(aIndent + ">\n");
  121.  
  122.   aIndent += "  ";
  123.   
  124.   for (var i = 0; i < aNode.childNodes.length; ++i)
  125.     dumpDOM(aNode.childNodes[i], aIndent);
  126. }
  127.  
  128. // convert nodeType constant into human readable form
  129. function nodeTypeToText (nodeType)
  130. {
  131.   switch (nodeType) {
  132.     case Node.ELEMENT_NODE:                 return "Element";
  133.     case Node.ATTRIBUTE_NODE:               return "Attribute";
  134.     case Node.TEXT_NODE:                    return "Text";
  135.     case Node.CDATA_SECTION_NODE:           return "CDATA Section";
  136.     case Node.ENTITY_REFERENCE_NODE:        return "Entity Reference";
  137.     case Node.ENTITY_NODE:                  return "Entity";
  138.     case Node.PROCESSING_INSTRUCTION_NODE:  return "Processing Instruction";
  139.     case Node.COMMENT_NODE:                 return "Comment";
  140.     case Node.DOCUMENT_NODE:                return "Document";
  141.     case Node.DOCUMENT_TYPE_NODE:           return "Document Type";
  142.     case Node.DOCUMENT_FRAGMENT_NODE:       return "Document Fragment";
  143.     case Node.NOTATION_NODE:                return "Notation";
  144.   }
  145.   return nodeType;
  146. }
  147.